package com.sparrow.common.util;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 数组工具类
 */
public class ArrayUtil {

    /**
     * 向 int 数组添加元素
     */
    public static int[] add(int[] array, int value) {
        if (array == null) {
            return new int[]{value};
        }
        int size = array.length;
        int[] newArray = Arrays.copyOf(array, size + 1);
        newArray[size] = value;
        return newArray;
    }

    /**
     * 向 long 数组添加元素
     */
    public static long[] add(long[] array, long value) {
        if (array == null) {
            return new long[]{value};
        }
        int size = array.length;
        long[] newArray = Arrays.copyOf(array, size + 1);
        newArray[size] = value;
        return newArray;
    }

    /**
     * 向泛型数组添加元素
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] add(T[] array, T value) {
        if (array == null) {
            T[] newArray = (T[]) Array.newInstance(value.getClass(), 1);
            newArray[0] = value;
            return newArray;
        }
        int size = array.length;
        T[] newArray = Arrays.copyOf(array, size + 1);
        newArray[size] = value;
        return newArray;
    }

    /**
     * 从数组中删除指定下标的元素
     */
    public static int[] remove(int[] array, int index) {
        if (array == null || index < 0 || index >= array.length) {
            return array;
        }
        int[] newArray = new int[array.length - 1];
        System.arraycopy(array, 0, newArray, 0, index);
        System.arraycopy(array, index + 1, newArray, index, array.length - index - 1);
        return newArray;
    }

    /**
     * 删除泛型数组中指定下标的元素
     */
    public static <T> T[] remove(T[] array, int index) {
        if (array == null || index < 0 || index >= array.length) {
            return array;
        }
        @SuppressWarnings("unchecked")
        T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length - 1);
        System.arraycopy(array, 0, newArray, 0, index);
        System.arraycopy(array, index + 1, newArray, index, array.length - index - 1);
        return newArray;
    }

    /**
     * 在数组指定位置插入元素
     */
    public static int[] insert(int[] array, int index, int value) {
        if (array == null) {
            return new int[]{value};
        }
        if (index < 0 || index > array.length) {
            throw new IndexOutOfBoundsException("index: " + index);
        }
        int[] newArray = new int[array.length + 1];
        System.arraycopy(array, 0, newArray, 0, index);
        newArray[index] = value;
        System.arraycopy(array, index, newArray, index + 1, array.length - index);
        return newArray;
    }

    /**
     * 合并两个数组
     */
    public static int[] merge(int[] a1, int[] a2) {
        if (a1 == null || a1.length == 0) return a2;
        if (a2 == null || a2.length == 0) return a1;
        int[] newArray = Arrays.copyOf(a1, a1.length + a2.length);
        System.arraycopy(a2, 0, newArray, a1.length, a2.length);
        return newArray;
    }

    /**
     * 去重（基于 HashSet）
     */
    public static int[] distinct(int[] array) {
        if (array == null || array.length <= 1) return array;
        Set<Integer> set = new HashSet<>();
        for (int v : array) set.add(v);
        return set.stream().mapToInt(Integer::intValue).toArray();
    }

    public static boolean contains(int[] array, int value) {
        if (array == null) return false;
        for (int v : array) {
            if (v == value) return true;
        }
        return false;
    }

    public static boolean contains(long[] array, long value) {
        if (array == null) return false;
        for (long v : array) {
            if (v == value) return true;
        }
        return false;
    }

    /**
     * 查找元素索引，不存在则返回 -1
     */
    public static int indexOf(int[] array, int value) {
        if (array == null) return -1;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == value) return i;
        }
        return -1;
    }

    /**
     * 数组是否为空
     */
    public static boolean isEmpty(Object array) {
        if (array == null) return true;
        return Array.getLength(array) == 0;
    }

    /**
     * 打印数组为字符串
     */
    public static String toString(int[] array) {
        return array == null ? "null" : Arrays.toString(array);
    }

    public static String toString(Object[] array) {
        return array == null ? "null" : Arrays.toString(array);
    }

}

